package com.langsin.blog.controller;

import com.langsin.blog.domain.User;
import com.langsin.blog.service.UserService;
import com.langsin.blog.service.impl.UserServiceImp;
import com.langsin.blog.util.JsonSerializeUtil;
import com.langsin.blog.util.MsgUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("user")
public class UserController {

    private UserService userService = new UserServiceImp();
    private String msg;

    @RequestMapping(value = "checkUserNameExist",method = {RequestMethod.POST,RequestMethod.GET},params = {"account"})
    public void isUserExist(@RequestParam("account")String account,HttpServletResponse response){
        msg = userService.userExist(account)+"";
        MsgUtil.sendMsgToClient(msg,response);
    }

    @RequestMapping(value="login",method = {RequestMethod.POST,RequestMethod.GET},params = {"account","password"})
    public void userLogin(@RequestParam("account") String account,@RequestParam("password") String password, HttpServletResponse response){
        msg = userService.loginCheck(account,password)+"";
        MsgUtil.sendMsgToClient(msg,response);
    }

    @RequestMapping(value="regist",method = RequestMethod.POST,params = {"account","password","nickname","email"})
    public void userRegist(@RequestParam("account")String account,@RequestParam("password")String password,
                       @RequestParam("nickname")String nickname,@RequestParam("email")String email,HttpServletResponse response){
        msg = String.valueOf(userService.registCheck(account,password,nickname,email,"root"));
        MsgUtil.sendMsgToClient(msg,response);
    }

    @RequestMapping(value="modify",method = RequestMethod.POST,params = {"account","prop","value"})
    public void userModify(@RequestParam("account")String account,@RequestParam("prop")String prop,
                       @RequestParam("value")String value,HttpServletResponse response){
        msg = String.valueOf(userService.userModify(account,prop,value));
        MsgUtil.sendMsgToClient(msg,response);
    }

    @RequestMapping(value="getUser",method = RequestMethod.POST,params = {"account"})
    public void userGet(@RequestParam("account")String account,HttpServletResponse response){
        User user = userService.userGet(account);
        msg = JsonSerializeUtil.convertToJsonString(user);
        MsgUtil.sendMsgToClient(msg,response);
    }
}
